package com.liao.member.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.liao.common.constant.SessionEnum;
import com.liao.common.dto.WechatLoginRequest;
import com.liao.common.entity.member.UserEntity;
import com.liao.common.utils.PageUtils;
import com.liao.common.utils.Result;
import com.liao.member.feign.MediaFeignService;
import com.liao.member.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Map;


/**
 * C端用户账户信息
 *
 * @author liao
 * @email 2858015963@qq.com
 * @date 2025-03-10 19:27:53
 */
@RestController
@RequestMapping("member/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private MediaFeignService mediaFeignService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public PageUtils<UserEntity> list(@RequestParam Map<String, Object> params) {
        return userService.queryPage(params);

    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    public UserEntity info(@PathVariable("userId") Integer userId) {
        return userService.getById(userId);

    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public void save(@RequestBody UserEntity user) {
        userService.save(user);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public void update(@RequestBody UserEntity user) {
        userService.updateById(user);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public void delete(@RequestBody Integer[] userIds) {
        userService.removeByIds(Arrays.asList(userIds));
    }


    @PostMapping("/wx/login")
    public UserEntity WxLogin(@RequestBody WechatLoginRequest request) {

        UserEntity wxlogin = userService.Wxlogin(request.getCode(), request.getPCode(), request.getRawData(), request.getSignature());
        return wxlogin;
    }

    /**
     * 获取用户名
     *
     * @return
     */
    @RequestMapping("/getKey")
    public UserEntity getKey() {

        StpUtil.login(10);

        UserEntity byId = userService.getById(10);

        StpUtil.getSession().set(SessionEnum.USER_INFO_KEY, byId);

        return byId;
    }

    ;

    /**
     * 获取个人信息（当前账号）
     */

    @RequestMapping("/info")
    public UserEntity update() {
        if (!StpUtil.isLogin()) {
            throw new RuntimeException("请先登录");
        }
        UserEntity user = (UserEntity) StpUtil.getSession().get(SessionEnum.USER_INFO_KEY);
        Integer userId = user.getUserId();

        return userService.getById(userId);

    }

    /**
     * 修改个人信息
     *
     * @param user 用户信息
     */
    @PostMapping("/profile")
    public void profile(@RequestBody UserEntity user) {
        if (!StpUtil.isLogin()) {
            throw new RuntimeException("请先登录");
        }
        UserEntity userEntity = (UserEntity) StpUtil.getSession().get(SessionEnum.USER_INFO_KEY);
        user.setUserId(userEntity.getUserId());
        userService.updateById(user);
    }

    /**
     * 修改头像
     */
    @PostMapping(value = "/profile/avatar", produces = "application/json;charset=UTF-8")
    public void changeAvatar(@RequestParam("file") MultipartFile file) {

        if (!StpUtil.isLogin()) {
            throw new RuntimeException("请先登录");
        }
        UserEntity userEntity = (UserEntity) StpUtil.getSession().get(SessionEnum.USER_INFO_KEY);
        Result<String> result = mediaFeignService.upload(file);
        if (result.getCode() == 200) {
            UserEntity updateUser = new UserEntity();
            updateUser.setAvatar(result.getData());
            updateUser.setUserId(userEntity.getUserId());
            userService.updateById(updateUser);

        } else {
            throw new RuntimeException("上传失败");
        }
    }
}
